<?php
namespace app\admin\controller;
use app\service\billService;
use app\service\lifeapiService;
use app\service\shopService;
use think\facade\Db;
use think\facade\View;

class Shop extends Common{
    //商铺列表
	public function shoplist(){
		$data=(new shopService())->shoplist();
		View::assign("list",$data);
		$number=count($data);
		View::assign("number",$number);
		return View::fetch('shop_list');
		
	}
    //店铺编辑
    public function shopEdit(){
        if(request()->isGet()){
            $shop_id = request()->param('shop_id');
            if(!isset($shop_id) && empty($shop_id)){
                $this->error('商户不存在');
            }
            $info = (new shopService())->shopIdInfo($shop_id);
            View::assign('info',$info);
            return View::fetch('shop_edit');
        }else{
            $data = request()->post();
            //var_dump($data);die;
            $file = request()->file('shop_logo');
            if(!empty($file)){
                unset($data['shop_logo']);
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shoplogo');
                if($info){
                    $data['shop_logo'] = $info->getSaveName(); 
                }
                echo $file->getError();
            }
            $result = (new shopService())->shopEdit($data);
            if($result){
                    $this->success('修改商铺成功');
            }else{
                    $this->error('修改商铺失败');
            }
        }
        return View::fetch('shop_edit');
    }

    public function shopAdd(){

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $file = request()->file('shop_logo');
            if(!empty($file)){
                unset($data['shop_logo']);
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shoplogo');
                if($info){
                    $data['shop_logo'] = $info->getSaveName(); 
                }
                echo $file->getError();
            }
            //判断账号名称是否重复        
            $info = (new shopService())->shopName($data['shop_name']);
            if($info){
                $this->error('商户已存在');
            }          
            //插入数据到数据库
            $result = (new shopService())->shopAdd($data);
            if($result){
                $this->success('添加商户成功',url('propertyAdd'));
            }else{
                $this->error('添加商户失败',url('propertyAdd'));
            }
        }
        return View::fetch('shop_add');
    }
    public function goodDel(){
        $goods_id=request()->get("goods_id");
        $goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
        if(empty($goodsinfo)){
            $this->error("商品不存在");
        }
        $res=Db::table("zz_goods")->where("goods_id",$goods_id)->delete();
        if($res){
            Db::table("zz_shopcart")->where("goods_id",$goods_id)->delete();
            Db::table("zz_goods_delete")->insert($goodsinfo);
            $url=url('goodlist');
            echo "<script>alert('商品删除成功');location.href='$url'</script>";
            exit;
            $this->success("商品删除成功",url('goodlist'));
        }else{
            $this->error("商品删除失败");
        }   
    }
    public function goodAdd(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            if(!empty($file)){
                
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shop'. DS .'upload');
                if($info){
                    $data['picture'] = $info->getSaveName(); 
                }else{
                    echo $file->getError();
                    die;
                }  
            }else{
                $this->error('请选择要上传的图片');
            }
            $shopinfo=Db::table("zz_shop")->where("shop_id",$data["shop_id"])->find();
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["salesprice"]=$data["price"];  
            $data["state"]="1";       
            //插入数据到数据库
            $res=Db::table("zz_goods")->insert($data);
            if($res){
                 $this->success('商品添加成功',url('goodlist'));
            }else{
                 $this->error('商品添加失败');
            }
        }else{
            $shoplist=Db::table("zz_shop")->order('shop_sort desc')->select()->toArray();
            View::assign("shoplist",$shoplist);

            $category=Db::table('zz_category')->order('sort desc')->select()->toArray();
            View::assign("category",$category);
            return View::fetch('good_add');
        }
        
    }

    public function goodedit(){
        if(request()->isGet()){
            if (!request()->has('goods_id') || empty(request()->param('goods_id'))) {
                $this->error('错误');
            }
            $goods_id = request()->param('goods_id');
            $info = (new shopService())->goodIdInfo($goods_id);
            View::assign('info',$info);
            if($info){
                return View::fetch('good_edit');
            }else{
                return json(['info' => '小区信息错误']);
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new shopService())->goodEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('商品修改成功',url('goodlist'));
            }else{
                $this->error('商品修改失败',url('goodlist'));
            }
        }
    }
    public function goodAddPro(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            if(!empty($file)){
                
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shop'. DS .'upload');
                if($info){
                    $data['picture'] = $info->getSaveName(); 
                }else{
                    echo $file->getError();
                    die;
                }  
            }else{
                $this->error('请选择要上传的图片');
            }
            $shopinfo=Db::table("zz_shop")->where("shop_id",$data["shop_id"])->find();
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["promotion_type"]=1;
            $data["state"]="1";       
            //插入数据到数据库
            $res=Db::table("zz_goods")->insert($data);
            if($res){
                 $this->success('商品添加成功',url('goodlist'));
            }else{
                 $this->error('商品添加失败');
            }
        }else{
            $shoplist=Db::table("zz_shop")->order('shop_sort desc')->select()->toArray();
            View::assign("shoplist",$shoplist);

            $category=Db::table('zz_category')->order('sort desc')->select()->toArray();
            View::assign("category",$category);
            return View::fetch('good_add_pro');
        }
        
    }
	//配置商家打印机
    public function printing(){
        if(request()->isGet()){
            if (!request()->has('id') || empty(request()->param('id'))) {
                $this->error('错误');
            }
            $id = request()->param('id');
            $info = (new shopService())->shopIdInfo($id);
            View::assign('info',$info);
            if($info){
                return View::fetch('shop_printing');
            }else{
                return json(['info' => '小区信息错误']);
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new shopService())->shopEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置打印机成功',url('shoplist'));
            }else{
                $this->error('配置打印机失败',url('shoplist'));
            }
        }
    }
    public function workingtime(){
        if(request()->isGet()){
            if (!request()->has('id') || empty(request()->param('id'))) {
                $this->error('错误');
            }
            $id = request()->param('id');
            $info = (new shopService())->shopIdInfo($id);
            View::assign('info',$info);
            if($info){
                return View::fetch('shop_workingtime');
            }else{
                return json(['info' => '小区信息错误']);
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new shopService())->shopEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('营业时间配置成功',url('shoplist'));
            }else{
                $this->error('营业时间配置失败',url('shoplist'));
            }
        }
    }

    //生活号会员
    public function userlist(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->userlist($where,$page);
        $number=(new shopService())->userlistnumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('user_list');
    }
    //商家订单列表
    public function orderlistoffline(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->orderlist($where,$page);
        $number=(new shopService())->ordernumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('order_list');
    }

    //商城订单列表
    public function orderlistonline(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->orderlistonline($where,$page);
        $number=(new shopService())->ordernumberonline($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('order_list_online');
    }

    public function ordergoodsonline(){
        $order_id=request()->get('order_id');
        $ordergoods=Db::table('zz_order_goods')->where('order_id',$order_id)->select()->toArray();
        $orderinfo=Db::table("zz_order_online")->where('order_id',$order_id)->find();
        View::assign("list",$ordergoods);
         View::assign("orderinfo",$orderinfo);
        return View::fetch('order_goods_online');
    }

    public function transferList(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->transferList($where,$page);
        $number=(new shopService())->transfernumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('transfer_list');
    }
    //账单列表
    public function billorderlist(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->billorderlist($where,$page);
        $number=(new shopService())->billordernumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('bill_order_list');
    }

    public function billorderinfo(){
        $id=request()->get('id');
        $billorder=Db::table('zz_bill_order')->where('id',$id)->find();
        $arrbillid=unserialize($billorder["bill_id"]);
        $owner_name =(new billService())->billOwnerName($billorder['out_room_id']);
        $data=array();
        foreach ($arrbillid as $key => $value) {
            $info=(new billService())->billIdinfo($value);
            $info['community_id'] =$billorder['community_name'];
           
            $info['owner_name'] =$owner_name;
            $data[]=$info;
        }
        View::assign("list",$data);
        return View::fetch('bill_order_info');
    }

     //商家优惠券列表
    public function couponlist(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->couponlist($where,$page);
        $number=(new shopService())->couponnumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('coupon_list');
    }

    //商品列表
    public function goodlist(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->goodlist($where,$page);
        $number=(new shopService())->goodnumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('good_list');
    }

    public function goodUp(){
        $goods_id=request()->get("goods_id");
        $goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
        //echo $goods_id;die;
        if(empty($goodsinfo)){
            $this->error("商品不存在");
        }
        $shopinfo=Db::table("zz_shop")->where("shop_id",$goodsinfo["shop_id"])->find();
        $res=Db::table("zz_goods")->where("goods_id",$goods_id)->update(["state"=>"1"]);
        if($res){
            $msg["title"]="恭喜您，您的商品已通过审核";
            $msg["user_id"]=$shopinfo["user_id"];//"2088102690661313";
            $msg["url"]="http://www.jfzhiwen.com/shop/merchant/goodlist.html";
            $msg["ctime"]=date('Y-m-d H:i:s');
            $msg["state"]="已通过";
            $msg["remark"]="";
            $api=new lifeapiService();
            $res=$api->verityresultmsg(json_encode($msg));

            $url=url('goodlist');
            echo "<script>alert('商品上架成功');location.href='$url'</script>";
            exit;
            
        }else{
            $this->error("商品上架失败");
        }   
    }
    public function goodCheckFail(){
        $goods_id=request()->get("goods_id");
        $goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
        if(empty($goodsinfo)){
            $this->error("商品不存在");
        }
        $res=Db::table("zz_goods")->where("goods_id",$goods_id)->update(["state"=>"2"]);
         
        if($res){
            Db::table("zz_shopcart")->where("goods_id",$goods_id)->delete();
            $shopinfo=Db::table("zz_shop")->where("shop_id",$goodsinfo["shop_id"])->find();
            $msg["title"]="您的商品审核失败";
            $msg["user_id"]=$shopinfo["user_id"];//"2088102690661313";
            $msg["url"]="http://www.jfzhiwen.com/shop/merchant/goodlist.html";
            $msg["ctime"]=date('Y-m-d H:i:s');
            $msg["state"]="审核失败";
            $msg["remark"]="";
            $api=new lifeapiService();
            $res=$api->verityresultmsg(json_encode($msg));

            $url=url('goodlist');
            echo "<script>alert('商品审核失败');location.href='$url'</script>";
            exit;
            
        }else{
            $this->error("操作失败");
        }  
    } 
    public function categorylist(){
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list=(new shopService())->categorylist($where,$page);
        $number=(new shopService())->categorynumber($where);
        View::assign("list",$list);
        View::assign("number",$number);
        return View::fetch('category_list');
    }

    //配置商家打印机
    public function categoryEdit(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result =Db::table("zz_category")->update($data);
            //判断数据是否插入成功
            if($result){
                $this->success('修改成功',url('categorylist'));
            }else{
                $this->error('修改失败',url('categorylist'));
            }
            
        }else{
            $category_id = request()->param('category_id');
            $info = Db::table("zz_category")->where("category_id",$category_id)->find();
            if(empty($info)){
                $this->error('错误');
            }
            View::assign('info',$info);
           
            return View::fetch('category_edit');
            
        }
    }
}